package com.ciban.smarthome.module.net.communication;

import com.ciban.smarthome.module.utils.ThreadManger;
import com.ciban.smarthome.module.entity.Host;

public class Communicate {
    public interface ConnListener {
        public void onSuccessed(String result);

        public void onFailed(String err);
    }

    private Network network;
    private String cmd;
    private ConnListener listener;

    public void sendData(Host host, String cmds, ConnListener listeners) {
        this.listener = listeners;
        this.cmd = cmds;
        //if (host.getCommMode() == Protocol.MODE_UDP) {
        network = new NetworkUDP(host.getIp(), host.getPort()
                + "");
        //}
        ThreadManger.getInstance().execute(new Runnable() {

            @Override
            public void run() {
                try {
                    String result = network.sendData(cmd);
                    if (listener != null)
                        listener.onSuccessed(result);
                } catch (Exception e) {
                    if (listener != null)
                        listener.onFailed(e.toString());
                }
            }
        });
    }
}
